package com.bilibili.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bilibili.entity.CommonResult;
import com.bilibili.entity.Video;
import com.bilibili.service.VideoService;
import com.bilibili.service.impl.VideoServiceImpl;

@SuppressWarnings("serial")
@WebServlet("/updateVideoState")
public class UpdateVideoStateServlet extends HttpServlet{
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("application/json; charset=utf-8");
		String videoId = request.getParameter("videoId");
		String state = request.getParameter("state");
		int video_id = 0,status = -1;
		try {
			video_id = Integer.parseInt(videoId);
		}catch (Exception e) {
			// TODO: handle exception
		}
		if(state.equals("上架")) {
			status = 1;
		}else {
			status = 2;
		}
		Video v = new Video();
		v.setVideoId(video_id);
		v.setStatus(status);
		VideoService vs = new VideoServiceImpl();
		CommonResult cr = new CommonResult();
		if(vs.updateVideostate(v)) {
			cr.setCode(200);
		}else {
			cr.setCode(409);
		}
		response.getWriter().write(cr.toJSONString());
	}
}
